package sonic.game.util;
/**
 * Vector2D
 *
 * A representation of a location in 2-dimensional space
 * Contains helper functions for calculating the length and normalising Vectors
 * 
 * @author Richard Greenlees
 */

public class Vector2D {

	public float x;
	public float y;
	
	public Vector2D()
	{
		x = 0.0f;
		y = 0.0f;
	}
	
	public Vector2D(float startX, float startY)
	{
		x = startX;
		y = startY;
	}

	public Vector2D(Vector2D startPos) {
		x = startPos.x;
		y = startPos.y;
	}
	
	/** Returns the result of subtracting b from a */
	public static Vector2D vectorSubtract(Vector2D a, Vector2D b)
	{
		Vector2D result = new Vector2D();
		result.x = a.x - b.x;
		result.y = a.y - b.y;
		return result;
	}
	
	/** Return the length of the supplied 2D Vector */
	public static float vSize(Vector2D a)
	{
		return (float) Math.sqrt((a.x * a.x) + (a.y * a.y));
	}
	
	/** Returns a normalised copy of the supplied 2D Vector */
	public static Vector2D normalise(Vector2D a)
	{
		Vector2D result = new Vector2D(a);
		float length = vSize(a);
		result.x /= length;
		result.y /= length;
		return result;
	}
}
